<template>
  <div class="mainmenu">
    <div class="category">
      <router-link class="class2" to="/zzt">首页</router-link>
      <img src="@/views/menudetail/images/qianjin.png" alt="" />
      <span class="class2">{{ menuinfo.class }}</span>
      <img src="@/views/menudetail/images/qianjin.png" alt="" />
      <span class="title0">{{ menuinfo.title }}</span>
    </div>
    <div class="title">{{ menuinfo.title }}</div>
    <div class="imgbox" @click="suofang()" ref="imgbox">
      <img :src="menuinfo.img" class="imgmain" alt="" />
    </div>
    <div class="box1">
      <span class="count1">{{ menuinfo.score }}</span>
      <span class="text1"> 综合评分</span>
      <span class="count1">{{ menuinfo["浏览次数"] }}</span>
      <span class="text1"> 人做过这道菜</span>
      <span class="shoucang" @click="fshoucang()">收藏</span>
    </div>
    <div class="motai" ref="motai">{{ shoucangtishi }}</div>
    <p class="hr"></p>
    <div class="uploader">
      <img :src="menuinfo.touxiang" alt="" class="upimg" />
      <span class="upname">{{ menuinfo.username }}</span>
      <p class="uptext">{{ menuinfo.miaoshu }}</p>
    </div>
    <div class="materials">
      <p class="title">用料</p>
      <div v-for="(item, index) in materialsArr" :key="index">
        <p>
          <span class="material">{{ item.material }}</span>
          <span class="count">{{ item.count }}</span>
        </p>
        <p class="hr"></p>
      </div>
    </div>
    <div class="stepbox">
      <p class="title">{{ menuinfo.title }}的做法</p>
      <div v-for="(item, index) in stepArr" :key="index" class="step">
        <span class="id1">{{ index + 1 }}</span>
        <span class="text">{{ item.text }}</span>
        <img :src="item.img" alt="" v-if="item.img" />
      </div>
    </div>
    <div class="tips" v-if="menuinfo.tip">
      <p class="title">小贴士</p>
      <p>{{ menuinfo.tip }}</p>
    </div>
    <div ref="workerbox" class="workerbox">
      <template v-if="showWorker">
        <Worker
          :menuTitle="this.menuinfo.title"
          :menuid="this.menuinfo.id"
        ></Worker>
      </template>
    </div>
  </div>
</template>

<script>
import Worker from "./components/Worker.vue";
import smaller from "../../images/smaller.png";
export default {
  data: () => {
    return {
      flag: 0, //图片放大
      showWorker: false,
      shoucangtishi: "",
    };
  },
  computed: {
    // 用料数据处理
    materialsArr() {
      let arr0 = [];
      let arr = this.menuinfo.yongliao.split(",");
      arr.map((el) => {
        el = el.split("-");
        let obj = {};
        obj["material"] = el[0];
        obj["count"] = el[1];
        arr0.push(obj);
      });
      return arr0;
    },
    // 步骤数据处理
    stepArr() {
      let arr0 = [];
      let arr = this.menuinfo.buzhou.split(",");
      let arr1 = this.menuinfo.buzouimg.split(",");
      arr.map((el, index) => {
        let obj = {};
        obj.text = el;
        obj.img = arr1[index];
        arr0.push(obj);
      });
      return arr0;
    },
  },
  components: {
    Worker,
  },
  props: ["menuinfo", "scflag"],
  methods: {
    // 菜谱图片的缩放
    suofang() {
      if (this.flag == 0) {
        // this.$refs.imgbox.style.backgroundImage = `url(${smaller})`;
        // this.$refs.imgbox.style.cursor = "pointer";

        // this.$refs.imgbox.style.cursor = `url(${smaller})`;
        // this.$refs.imgbox.style.cursor = `url(require('../../images/smaller.png')),auto`;
        this.$refs.imgbox.style.height =
          this.$refs.imgbox.children[0].height + "px";
        this.flag = 1;
      } else {
        this.$refs.imgbox.style.cursor = `url(require('../../images/bigger.png')),auto`;
        this.$refs.imgbox.style.height = "420px";
        this.flag = 0;
      }
    },
    // 滚动条滚动到一定位置出现其他用户作品
    workerLoader() {
      if (this.$refs.workerbox) {
        if (window.scrollY > this.$refs.workerbox.offsetTop - 720) {
          this.showWorker = true;
        }
      }
    },
    // 收藏菜谱
    fshoucang() {
      let islogin = window.localStorage.getItem("islogin");
      // 已登录
      if (islogin) {
        // 未收藏
        if (this.scflag == 1) {
          this.$axios("/wxw/wstorecook", {
            params: {
              cookid: this.menuinfo.id,
            },
          }).then((res) => {
            if (res.data.code == 2101) {
              this.$refs.motai.style.width = "100px";
              this.shoucangtishi = "收藏成功";
              this.$refs.motai.style.color = "green";
              this.$refs.motai.style.display = "block";
              this.scflag = 0;
              setTimeout(() => {
                this.$refs.motai.style.display = "none";
              }, 1000);
            }
          });
        } else {
          this.$refs.motai.style.width = "150px";
          this.shoucangtishi = "您已经收藏过了";
          this.$refs.motai.style.color = "red";
          this.$refs.motai.style.display = "block";
          setTimeout(() => {
            this.$refs.motai.style.display = "none";
          }, 1000);
        }
      } else {
        this.$router.push("/login");
      }
    },
  },
  mounted() {
    // 懒加载
    window.addEventListener("scroll", this.workerLoader);
  },
};
</script>


<style scoped lang="scss">
.category {
  font-size: 16px;
  .class2 {
    color: #dd3915;
    cursor: pointer;
  }
  .title0 {
    color: #909090;
    cursor: pointer;
  }
  img {
    width: 8px;
    margin: 0 5px;
  }
}
.mainmenu {
  width: 660px;
  color: #222;
  font-size: 18px;
}
.hr {
  height: 1px;
  background-color: #eee;
}
.title {
  font-size: 34px;
  font-weight: bolder;
  margin: 16px 0;
  width: 630px;
}
.imgbox {
  width: 660px;
  height: 420px;
  overflow: hidden;
  transition: all 0.5s;
  cursor: url("../../images/bigger.png"), auto;
}
.imgmain {
  width: 660px;
}

.box1 {
  padding: 20px 0;
  position: relative;
  .count1 {
    font-size: 20px;
    color: #dd3915;
    margin-left: 10px;
  }
  .text1 {
    color: #909090;
    font-size: 14px;
  }
  .shoucang {
    display: inline-block;
    background-color: #dd3915;
    font-size: 15px;
    color: #fff;
    width: 112px;
    height: 44px;
    line-height: 44px;
    text-align: center;
    position: absolute;
    right: 10px;
    top: 10px;
    cursor: pointer;
  }
}

.motai {
  background-color: rgba(255, 255, 255);
  height: 30px;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  margin: auto;
  position: fixed;
  z-index: 10;
  font-size: 14px;
  line-height: 30px;
  text-align: center;
  border-radius: 10px;
  display: none;
}

.uploader {
  .upimg {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    vertical-align: middle;
    margin: 20px 10px;
  }
  .upname {
    font-size: 18px;
  }
  .uptext {
    font-size: 18px;
  }
}

.materials {
  .title {
    color: #c0ae7d;
    font-size: 24px;
    margin: 30px 0 20px;
  }
  .material {
    display: inline-block;
    width: 420px;
    font-size: 18px;
    margin: 15px 0;
  }
  .count {
    display: inline-block;
    width: 180px;
    font-size: 18px;
  }
}

.stepbox {
  .title {
    font-size: 24px;
    color: #c0ae7d;
    margin: 30px 0;
  }
  .step {
    display: flex;
    width: 660px;
    margin-bottom: 20px;
    padding-right: 20px;
    min-height: 30px;
    .id1 {
      font-size: 24px;
      color: #c0ae7d;
    }
    .text {
      width: 290px;
      margin: 0 30px;
    }
    img {
      width: 300px;
    }
  }
}

.tips {
  background-color: #eee;
  padding: 10px 40px 20px;
  margin: 30px 0 70px;
  font-size: 15px;
  line-height: 25px;
  .title {
    color: #000;
    font-size: 16px;
  }
}
</style>
